﻿Imports System.Data.OleDb
Imports System.Data
Public Class Login
    Inherits Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        'RegisterHyperLink.NavigateUrl = "Register.aspx"

        Dim returnUrl = HttpUtility.UrlEncode(Request.QueryString("ReturnUrl"))
        If Not String.IsNullOrEmpty(returnUrl) Then
            ' RegisterHyperLink.NavigateUrl &= "?ReturnUrl=" & returnUrl
        End If
    End Sub



    Protected Sub sitelogin_LoggedIn(sender As Object, e As EventArgs) Handles sitelogin.LoggedIn
        ' check if a profile exist with the username
        'create one if one does not exist go to 
        Dim txtuser As TextBox = DirectCast(sitelogin.FindControl("UserName"), TextBox)

        Dim tUsername As String = txtuser.Text
        Dim conn = New OleDbConnection(System.Configuration.ConfigurationManager.ConnectionStrings("fishconn").ConnectionString)
        conn.Open()
        Dim cmd As OleDbCommand = New OleDbCommand("SELECT [FirstName] FROM tblProfile WHERE (((tblProfile.UserName) Like [?]));", conn)

        cmd.Parameters.AddWithValue("@username", tUsername)
        Dim rs As OleDbDataReader = cmd.ExecuteReader
        If rs.Read() Then
            ' Response.Redirect("~/")
        Else

            Dim SqlString As String = "Insert into tblProfile(UserName,ProfilePicture,ProfilePicturesmall) Values (@username, @photo, @smallphoto)"
            If conn.State = ConnectionState.Closed Then
                conn.Open()
            End If

            Dim cmdins As OleDbCommand = New OleDbCommand(SqlString, conn)
            cmdins.CommandType = CommandType.Text
            cmdins.Parameters.AddWithValue("@username", tUsername)
            cmdins.Parameters.AddWithValue("@photo", "Images\noimage.jpg")
            cmdins.Parameters.AddWithValue("@smallphoto", "Images\noimage.jpg")
            If cmdins.ExecuteNonQuery() = 1 Then
                Response.Redirect("~/Account/Editprofile.aspx")
            End If
            conn.Close()


        End If



    End Sub
End Class